/**********************************************************
*
*   @file    useri2cReg.c
*   @author  Derrick Wang
*   @time    2018-4-26
*		@brief   i2c协议和外部交互的函数
*
*
***********************************************************/


#include "main.h"
#include "stm32f0xx_hal.h"
#include "userI2c.h"
#include "userI2cReg.h"
#include "userkeyboard.h"


struct userI2cRegStruct userI2cRegList[]=
{
	//通用寄存器
	{REG_CHECK,1,CMD_RT_TRANSMIT},
	{REG_VERSION,1,CMD_RT_TRANSMIT},
	{REG_LED,1,CMD_RT_RECEIVE},
	{REG_RESET,0,CMD_RT_RECEIVE},
	//模块专用寄存器
	{REG_KEY0_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY1_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY2_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY3_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY4_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY5_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY6_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY7_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY8_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY9_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYA_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYB_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYC_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYD_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYE_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYF_SHORT_RELEASE,1,CMD_RT_TRANSMIT},
	
	{REG_KEY0_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY1_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY2_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY3_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY4_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY5_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY6_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY7_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY8_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEY9_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYA_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYB_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYC_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYD_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYE_LONG_RELEASE,1,CMD_RT_TRANSMIT},
	{REG_KEYF_LONG_RELEASE,1,CMD_RT_TRANSMIT},

	{REG_KEY0_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY1_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY2_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY3_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY4_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY5_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY6_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY7_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY8_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEY9_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEYA_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEYB_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEYC_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEYD_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEYE_LONG,1,CMD_RT_TRANSMIT},
	{REG_KEYF_LONG,1,CMD_RT_TRANSMIT},	
	
	{REG_ALLKEY_SHORT_RELEASE,2,CMD_RT_TRANSMIT},
	{REG_ALLKEY_LONG_RELEASE,2,CMD_RT_TRANSMIT},
	{REG_ALLKEY_LONG,2,CMD_RT_TRANSMIT},
};

//获取要发送的数据，返回-1为错误，其他值为返回的数据长度
//cmd为命令，buf用来存放获取到的数据
uint8_t user_I2cGetTransmitData(uint8_t reg,uint8_t *buf)
{
	uint16_t tmpValue;
	switch(reg)
	{
		case REG_CHECK:
			buf[0]=REG_CHECK_REPLY;
			break;
		case REG_VERSION:
			buf[0]=VERSION;
			break;
		case REG_KEY0_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY0);
			break;
		case REG_KEY1_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY1);
			break;
		case REG_KEY2_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY2);
			break;
		case REG_KEY3_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY3);
			break;
		case REG_KEY4_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY4);
			break;
		case REG_KEY5_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY5);
			break;
		case REG_KEY6_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY6);
			break;
		case REG_KEY7_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY7);
			break;
		case REG_KEY8_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY8);
			break;
		case REG_KEY9_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEY9);
			break;
		case REG_KEYA_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEYA);
			break;
		case REG_KEYB_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEYB);
			break;
		case REG_KEYC_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEYC);
			break;
		case REG_KEYD_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEYD);
			break;
		case REG_KEYE_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEYE);
			break;
		case REG_KEYF_SHORT_RELEASE:
			buf[0]=user_keyShortRelease(KEYF);
			break;

		case REG_KEY0_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY0);
			break;
		case REG_KEY1_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY1);
			break;
		case REG_KEY2_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY2);
			break;
		case REG_KEY3_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY3);
			break;
		case REG_KEY4_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY4);
			break;
		case REG_KEY5_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY5);
			break;
		case REG_KEY6_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY6);
			break;
		case REG_KEY7_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY7);
			break;
		case REG_KEY8_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY8);
			break;
		case REG_KEY9_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEY9);
			break;
		case REG_KEYA_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEYA);
			break;
		case REG_KEYB_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEYB);
			break;
		case REG_KEYC_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEYC);
			break;
		case REG_KEYD_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEYD);
			break;
		case REG_KEYE_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEYE);
			break;
		case REG_KEYF_LONG_RELEASE:
			buf[0]=user_keyLongRelease(KEYF);
			break;
		
		
		case REG_KEY0_LONG:
			buf[0]=user_keyLongHold(KEY0);
			break;
		case REG_KEY1_LONG:
			buf[0]=user_keyLongHold(KEY1);
			break;
		case REG_KEY2_LONG:
			buf[0]=user_keyLongHold(KEY2);
			break;
		case REG_KEY3_LONG:
			buf[0]=user_keyLongHold(KEY3);
			break;
		case REG_KEY4_LONG:
			buf[0]=user_keyLongHold(KEY4);
			break;
		case REG_KEY5_LONG:
			buf[0]=user_keyLongHold(KEY5);
			break;
		case REG_KEY6_LONG:
			buf[0]=user_keyLongHold(KEY6);
			break;
		case REG_KEY7_LONG:
			buf[0]=user_keyLongHold(KEY7);
			break;
		case REG_KEY8_LONG:
			buf[0]=user_keyLongHold(KEY8);
			break;
		case REG_KEY9_LONG:
			buf[0]=user_keyLongHold(KEY9);
			break;
		case REG_KEYA_LONG:
			buf[0]=user_keyLongHold(KEYA);
			break;
		case REG_KEYB_LONG:
			buf[0]=user_keyLongHold(KEYB);
			break;
		case REG_KEYC_LONG:
			buf[0]=user_keyLongHold(KEYC);
			break;
		case REG_KEYD_LONG:
			buf[0]=user_keyLongHold(KEYD);
			break;
		case REG_KEYE_LONG:
			buf[0]=user_keyLongHold(KEYE);
			break;
		case REG_KEYF_LONG:
			buf[0]=user_keyLongHold(KEYF);
			break;



		
		
		case REG_ALLKEY_SHORT_RELEASE:
			tmpValue=user_allKeyShortRelease();
			buf[0]=(tmpValue>>8)&0xff;
			buf[1]=tmpValue&0xff;
			break;
		case REG_ALLKEY_LONG_RELEASE:
			tmpValue=user_allKeyLongRelease();
			buf[0]=(tmpValue>>8)&0xff;
			buf[1]=tmpValue&0xff;			
			break;
		case REG_ALLKEY_LONG:
			tmpValue=user_allKeyLongHold();
			buf[0]=(tmpValue>>8)&0xff;
			buf[1]=tmpValue&0xff;			
			break;

	}
	return 1;
}
//命令执行函数
void user_I2cExecuteCallBack(uint8_t reg,uint8_t *buf,uint8_t size)
{
	switch(reg)
	{
		case REG_LED:
			if(buf[0]==0x00)
			{
				LED_OFF();
			}
			else
			{
				LED_ON();
			}
			break;
		case REG_RESET:
			NVIC_SystemReset();
			break;
	}
}
